/* globals _ */

define([], function () {

    "use strict";

    var exports,
		_guid, _date, _form, _ko, _notify, _image;

    _guid = (function () {
        function s4() {
            return Math.floor((1 + Math.random()) * 0x10000)
                       .toString(16)
                       .substring(1);
        }
        return function () {
            return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
                   s4() + '-' + s4() + s4() + s4();
        };
    })();

    _date = {
        getDate: function (val) {

            if (val instanceof Date) return val;
            if (!val) return new Date();

            var dateParts = val.split("/");

            if (dateParts.length == 2) {
                return new Date(dateParts[1], (dateParts[0] - 1), 1);
            }

            if (dateParts.length == 3) {
                return new Date(dateParts[2], (dateParts[0] - 1), dateParts[1]);
            }

            return new Date();
        },
        getYear: function (val) {
            var d = this.getDate(val);
            var year = d.getFullYear();
            return year;
        },
        getMonth: function (val) {
            var d = this.getDate(val);
            var month = d.getMonth() + 1;
            return month;
        },
        getDay: function (val) {
            var d = this.getDate(val);
            var day = d.getDate();
            return day;
        },
        getMonthYear: function (val) {
            var d = this.getDate(val);
            return this.getMonth(d) + "/" + this.getYear(d);
        },
        getShortDate: function (val) {
            var d = this.getDate(val);
            d = this.getMonth(d) + "/" + this.getDay(d) + "/" + this.getYear(d);
            return d;
        },
        getTerm: function (val) {
            var d = this.getDate(val);
            var curYear = this.getYear(d);
            var curMonth = this.getMonth(d);
            if (curMonth <= 5) return "Spring " + curYear;
            else if (curMonth <= 8) return "Summer " + curYear;
            else return "Fall " + curYear;
        },
        getMonthName: function (number) {
            //NOTE: Below code can be used in enumerations
            //var months = { January: 1, February: 2, March: 3, April: 4, May: 5, };
            //var test = months.April; test = months.$2;
            //return months[number];
            var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
            return monthNames[number];
        },
        toDateString: function (date) {
            if (date == null) return void 0;
            return moment.utc(date).format("MM/DD/YYYY");
        },
        toDateTimeString: function (date) {
            if (date == null) return void 0;
            return moment.utc(date).format("MM/DD/YYYY hh:mm:ss");
        },
        fromNow: function(dateTimeString) {
            return moment.utc(dateTimeString).fromNow();
        },
        min: function() {
            return moment('1/1/1900', "MM/DD/YYYY").format("MM/DD/YYYY");
        },
        now: function () {
            return moment().format("MM/DD/YYYY");
        },
        tomorrow: function() {
            return moment().add('days', 1).format("MM/DD/YYYY");
        },
        yesterday: function() {
            return moment().add('days', -1).format("MM/DD/YYYY");
        }
    };

    _form = {
        reset: function ($form) {
            $form.find('input:text, input:password, input:file, select, textarea').val('').change();
            $form.find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
        },
        findSuitableTextWidth: function (val) {
            var inputDefaultWidth = 206/*px*/,
			singleCharacterWidth = 8/*px*/,
			inputInternalWidth = val ? val.length * singleCharacterWidth : 0,
			inputWidth = inputInternalWidth > inputDefaultWidth ? inputInternalWidth : inputDefaultWidth;
            return inputWidth;
        }
    };

    _ko = {
        getIdNamePair: function (id, name) {
            return {
                id: ko.observable(id),
                name: ko.observable(name)
            };
        },
        getIdNamePairs: function (pairs) {
            var self = this;
            var koPairs = [];
            $.each(pairs, function (row, value) {
                koPairs.push(self.getIdNamePair(value.id, value.name));
            });
            return koPairs;
        },
        findKoIdNamePair: function (array, id) {
            return _.find(array, function (a) { return a.id() === id; });
        },
        disposeModal: function ($modal) {
            $modal.modal('hide');
            ko.cleanNode($modal[0]);
        }
    };

    //TODO: http://notifyjs.com/
    _notify = {
        info: function (message) {
            alert(message);
            //    $.notify({ title: "Information", text: message }, { style: 'metro', className: 'success', globalPosition: 'top right', hideDuration: 1000, });
        },

        error: function (message) {
            alert(message);
        //    $.notify({ title: "Error", text: message }, { style: 'metro', className: 'success', globalPosition: 'top right', hideDuration: 1000, });
        }
    },

    _image = {
        getDefaultImageData: function () {
            return "";
        },
        checkOrDefault: function (data) {
            var self = this;
            if (!data || data.length < 100) return self.getDefaultImageData();
            return data;
        },
        drawImage: function (elm, data) {
            var self = this;
            if (!data || data.length < 100) data = self.getDefaultImageData();
            var ctx = elm.getContext("2d");
            var image = new Image();
            image.src = data;
            image.onload = function () {
                ctx.drawImage(image, 0, 0, 150, 187);
            };
        }
    };

    exports = {
        guid: _guid,
        date: _date,
        form: _form,
        ko: _ko,
        notify: _notify,
        image: _image,
        toJS: function (json) {
            return JSON.parse(JSON.stringify(json));
        },
        isUidNullOrEmpty: function (val) {
            return !val || val == '00000000-0000-0000-0000-000000000000';
        },
        isSmartPhone: function () {
            return $(window).width() <= 480;
        }
    };

    return exports;

});